<%@ Page Language="vb" CodeFile="settingsProduct.aspx.vb" Inherits="settingsProduct_aspx_vb" %>

<%
    rowNumber = 0
    If Request.QueryString("do") = Nothing Then
        subDo = Request.QueryString("subDo")
        productID = Request.QueryString("productID")
%>
<script type="text/javascript">
    function confirmSubmit() {
        if (window.confirm('Are you sure you want to delete the product?')) {
            location.href = 'settingsProduct.aspx?do=delete&productID=<%= productID %>';
            return false;
        }
        else {
            return false;
        }
    }
  </script>
<%
        If Len(productID) > 0 Then 
            productID = CLng(productID)
            dbOpen()
        RecSet = Conn.Execute("Select countryID, product, riskCarrier, allowInvoiceDistancePenalty, allowInvoiceDurationPenalty, allowInvoiceServiceAccountBalance, allowInvoiceAdminFee,partnerSubvention, (Select Count(productID) From tblVehicle Where productID = " & productID & ") As vehicleCount From tblProduct Where productID = " & productID & "")
            If RecSet.EOF Then 
                productID = ""
            Else
                dbCountryID = RecSet.Fields("countryID").Value
                dbProduct = RecSet.Fields("product").Value
                dbRiskCarrier = RecSet.Fields("riskCarrier").Value
                dbAllowInvoiceDistancePenalty = RecSet.Fields("allowInvoiceDistancePenalty").Value
                dbAllowInvoiceDurationPenalty = RecSet.Fields("allowInvoiceDurationPenalty").Value
                dbAllowInvoiceServiceAccountBalance = RecSet.Fields("allowInvoiceServiceAccountBalance").Value
                dbAllowInvoiceAdminFee = RecSet.Fields("allowInvoiceAdminFee").Value
                dbVehicleCount = RecSet.Fields("vehicleCount").Value
            dbpartnerSubvention = RecSet.Fields("partnerSubvention").Value
            
            End IF
            RecSet.Close()
            RecSet = Nothing
            dbClose()
        End IF
        If Len(dbRiskCarrier) > 0 Then 
            If CInt(dbRiskCarrier) = 0 Then 
                dbRiskCarrier = 2
            End IF
        End IF
        If Len(dbAllowInvoiceDistancePenalty) > 0 Then 
            If CInt(dbAllowInvoiceDistancePenalty) = 0 Then 
                dbAllowInvoiceDistancePenalty = 2
            End IF
        End IF
        If Len(dbAllowInvoiceDurationPenalty) > 0 Then 
            If CInt(dbAllowInvoiceDurationPenalty) = 0 Then 
                dbAllowInvoiceDurationPenalty = 2
            End IF
        End IF
        If Len(dbAllowInvoiceServiceAccountBalance) > 0 Then 
            If CInt(dbAllowInvoiceServiceAccountBalance) = 0 Then 
                dbAllowInvoiceServiceAccountBalance = 2
            End IF
        End IF
        If Len(dbAllowInvoiceAdminFee) > 0 Then 
            If CInt(dbAllowInvoiceAdminFee) = 0 Then 
                dbAllowInvoiceAdminFee = 2
            End IF
        End IF
        Over()
        strJS = ""
        strJS = strJS + "[""countryID"", ""<1""],"
        strJS = strJS + "[""product"", ""==''""],"
        strJS = strJS + "[""riskCarrier"", ""<1""],"
        strJS = strJS + "[""allowInvoiceDistancePenalty"", ""<1""],"
        strJS = strJS + "[""allowInvoiceDurationPenalty"", ""<1""],"
        strJS = strJS + "[""allowInvoiceServiceAccountBalance"", ""<1""],"
        strJS = strJS + "[""allowInvoiceAdminFee"", ""<1""],"
        checkFields()
        Response.Write("<table cellpadding='0' cellspacing='0' border='0' width='100%'>")
        Response.Write("<tr>")
        Response.Write("<td valign='top'>")
        Response.Write("<table cellpadding='2' cellspacing='2' width='450'>")
        Response.Write("<tr><td class='headline' colspan='3'>PRODUCT SETTINGS<font color='#fcbc00'>" & subDo & "</font><hr class='hrHeadline'></td></tr>")
        Response.Write("<form action='settingsProduct.aspx?do=save&productID=" & productID & "' method='post' onsubmit=""javascript:return checkFields();"">")
        Response.Write("<tr>")
        Response.Write("<td class='formBold'>Country:</td>")
        Response.Write("<td align='right'>")
        Response.Write("<select name='countryID' id='countryID'>")
        Response.Write("<option value=''> Select country  ")
        dbOpen()
        RecSet = Conn.Execute("Select countryID, country From tblCountry")
        If Not RecSet.EOF Then 
            Do Until RecSet.EOF
                countryID = RecSet.Fields("countryID").Value
                country = RecSet.Fields("country").Value
                Response.Write("<option value='" & countryID & "'")
                If dbCountryID = CStr(countryID) Then 
                    Response.Write(" selected='selected'")
                End IF
                Response.Write("> " & country & " ")
                RecSet.MoveNext()
            Loop
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        Response.Write("<option value=''> -------------------------------------- ")
        Response.Write("</select>")
        Response.Write("</td>")
        Response.Write("<td><div id='error_countryID' class='edit_error'>&laquo; OBS!</div></td>")
        Response.Write("</tr>")
        Response.Write("<tr><td class='formBold'>Product:</td><td align='right'><input type='text' name='product' id='product' size='40' maxlength='50' value='" & dbProduct & "'></td><td><div id='error_product' class='edit_error'>&laquo; OBS!</div></td></tr>")
    Response.Write("<tr>")
    Response.Write("<tr><td class='formBold'>PartnerSubvention:</td><td align='right'><input type='text' name='partnerSubvention' id='partnerSubvention' size='40' maxlength='50' value='" & dbpartnerSubvention & "'></td><td></td></tr>")
        Response.Write("<td class='formBold'>Do we carry the risk:</td>")
        Response.Write("<td align='right'>")
        Response.Write("<select name='riskCarrier' id='riskCarrier'>")
        Response.Write("<option value=''> Select choice  ")
        Response.Write("<option value='1'")
        If CInt(dbRiskCarrier) = 1 Then 
            Response.Write(" selected='selected'")
        End IF
        Response.Write("> Yes ")
        Response.Write("<option value='2'")
        If CInt(dbRiskCarrier) = 2 Then 
            Response.Write(" selected='selected'")
        End IF
        Response.Write("> No ")
        Response.Write("<option value=''> -------------------------------------- ")
        Response.Write("</select>")
        Response.Write("</td>")
        Response.Write("<td><div id='error_riskCarrier' class='edit_error'>&laquo; OBS!</div></td>")
        Response.Write("</tr>")
        Response.Write("<tr>")
        Response.Write("<td class='formBold'>Allow distance penalty invoices:</td>")
        Response.Write("<td align='right'>")
        Response.Write("<select name='allowInvoiceDistancePenalty' id='allowInvoiceDistancePenalty'>")
        Response.Write("<option value=''> Select choice  ")
        Response.Write("<option value='1'")
        If CInt(dbAllowInvoiceDistancePenalty) = 1 Then 
            Response.Write(" selected='selected'")
        End IF
        Response.Write("> Yes ")
        Response.Write("<option value='2'")
        If CInt(dbAllowInvoiceDistancePenalty) = 2 Then 
            Response.Write(" selected='selected'")
        End IF
        Response.Write("> No ")
        Response.Write("<option value=''> -------------------------------------- ")
        Response.Write("</select>")
        Response.Write("</td>")
        Response.Write("<td><div id='error_allowInvoiceDistancePenalty' class='edit_error'>&laquo; OBS!</div></td>")
        Response.Write("</tr>")
        Response.Write("<tr>")
        Response.Write("<td class='formBold'>Allow duration penalty invoices:</td>")
        Response.Write("<td align='right'>")
        Response.Write("<select name='allowInvoiceDurationPenalty' id='allowInvoiceDurationPenalty'>")
        Response.Write("<option value=''> Select choice  ")
        Response.Write("<option value='1'")
        If CInt(dbAllowInvoiceDurationPenalty) = 1 Then 
            Response.Write(" selected='selected'")
        End IF
        Response.Write("> Yes ")
        Response.Write("<option value='2'")
        If CInt(dbAllowInvoiceDurationPenalty) = 2 Then 
            Response.Write(" selected='selected'")
        End IF
        Response.Write("> No ")
        Response.Write("<option value=''> -------------------------------------- ")
        Response.Write("</select>")
        Response.Write("</td>")
        Response.Write("<td><div id='error_allowInvoiceDurationPenalty' class='edit_error'>&laquo; OBS!</div></td>")
        Response.Write("</tr>")
        Response.Write("<tr>")
        Response.Write("<td class='formBold'>Allow Service Account balance invoices:</td>")
        Response.Write("<td align='right'>")
        Response.Write("<select name='allowInvoiceServiceAccountBalance' id='allowInvoiceServiceAccountBalance'>")
        Response.Write("<option value=''> Select choice  ")
        Response.Write("<option value='1'")
        If CInt(dbAllowInvoiceServiceAccountBalance) = 1 Then 
            Response.Write(" selected='selected'")
        End IF
        Response.Write("> Yes ")
        Response.Write("<option value='2'")
        If CInt(dbAllowInvoiceServiceAccountBalance) = 2 Then 
            Response.Write(" selected='selected'")
        End IF
        Response.Write("> No ")
        Response.Write("<option value=''> -------------------------------------- ")
        Response.Write("</select>")
        Response.Write("</td>")
        Response.Write("<td><div id='error_allowInvoiceServiceAccountBalance' class='edit_error'>&laquo; OBS!</div></td>")
        Response.Write("</tr>")
        Response.Write("<tr>")
        Response.Write("<td class='formBold'>Allow admin fee invoices:</td>")
        Response.Write("<td align='right'>")
        Response.Write("<select name='allowInvoiceAdminFee' id='allowInvoiceAdminFee'>")
        Response.Write("<option value=''> Select choice  ")
        Response.Write("<option value='1'")
        If CInt(dbAllowInvoiceAdminFee) = 1 Then 
            Response.Write(" selected='selected'")
        End IF
        Response.Write("> Yes ")
        Response.Write("<option value='2'")
        If CInt(dbAllowInvoiceAdminFee) = 2 Then 
            Response.Write(" selected='selected'")
        End IF
        Response.Write("> No ")
        Response.Write("<option value=''> -------------------------------------- ")
        Response.Write("</select>")
        Response.Write("</td>")
        Response.Write("<td><div id='error_allowInvoiceAdminFee' class='edit_error'>&laquo; OBS!</div></td>")
        Response.Write("</tr>")
        Response.Write("<tr>")
        Response.Write("<td align='right' colspan='2'>")
        If CInt(dbVehicleCount) = 0 And CDbl(fragusLeasingUserTypeID) = 1 And Len(productID) > 0 Then 
            Response.Write("<input type='button' value='Delete product' onClick='return confirmSubmit()'>&nbsp; ")
        End IF
        Response.Write("<input type='submit' value='Save'>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Response.Write("</form>")
        Response.Write("<tr><td colspan='3'><hr class='hrHeadline'></td></tr>")
        Response.Write("</table>")
        Response.Write("</td>")
        Response.Write("<td width='100'></td>")
        Response.Write("<td valign='top'>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Response.Write("<tr><td><br></td></tr>")
        Response.Write("<tr>")
        Response.Write("<td valign='top' colspan='3'>")
        Response.Write("<table cellpadding='2' cellspacing='2' width='100%'>")
        Response.Write("<tr><td class='headline' colspan='11'>EXISTING PRODUCTS<hr class='hrHeadline'></td></tr>")
        Response.Write("<tr bgcolor='#eeeeee'>")
        Response.Write("<td class='formBold'>Product</td>")
        Response.Write("<td class='formBold'>PartnerSubvention</td>")
        Response.Write("<td class='formBold'>Country</td>")
        Response.Write("<td class='formBold'>Risk Carrier</td>")
        Response.Write("<td class='formBold'>Distance Penalty</td>")
        Response.Write("<td class='formBold'>Duration Penalty</td>")
        Response.Write("<td class='formBold'>Service Account</td>")
        Response.Write("<td class='formBold'>Admin Fee</td>")
        Response.Write("<td class='formBold'>Lease Contracts</td>")
        Response.Write("<td class='formBold'></td>")
        Response.Write("<td class='formBold'></td>")
        Response.Write("<td class='formBold'></td>")
        Response.Write("</tr>")
        rowNumber = 1
        dbOpen()
    RecSet = Conn.Execute("Select p.productID, p.product, p.riskCarrier, p.allowInvoiceDistancePenalty, p.allowInvoiceDurationPenalty, p.allowInvoiceServiceAccountBalance, p.allowInvoiceAdminFee, p.partnerSubvention,c.country, (Select Count(vehicleID) From tblVehicle Where productID = p.productID) As vehicleCount From (tblProduct p INNER JOIN tblCountry c ON c.countryID = p.countryID) Order by p.product")
        If RecSet.EOF Then 
            Response.Write("<tr><td><font color='red'>No products found.</font></td></tr>")
        Else
            Do Until RecSet.EOF
                productID = RecSet.Fields("productID").Value
                product = RecSet.Fields("product").Value
                riskCarrier = RecSet.Fields("riskCarrier").Value
                allowInvoiceDistancePenalty = RecSet.Fields("allowInvoiceDistancePenalty").Value
                allowInvoiceDurationPenalty = RecSet.Fields("allowInvoiceDurationPenalty").Value
                allowInvoiceServiceAccountBalance = RecSet.Fields("allowInvoiceServiceAccountBalance").Value
                allowInvoiceAdminFee = RecSet.Fields("allowInvoiceAdminFee").Value
                country = RecSet.Fields("country").Value
            vehicleCount = RecSet.Fields("vehicleCount").Value
            partnerSubvention = RecSet.Fields("partnerSubvention").Value
                If CInt(riskCarrier) = 1 Then 
                    riskCarrier = "Yes"
                Else
                    riskCarrier = "No"
                End IF
                If CInt(allowInvoiceDistancePenalty) = 1 Then 
                    allowInvoiceDistancePenalty = "Yes"
                Else
                    allowInvoiceDistancePenalty = "No"
                End IF
                If CInt(allowInvoiceDurationPenalty) = 1 Then 
                    allowInvoiceDurationPenalty = "Yes"
                Else
                    allowInvoiceDurationPenalty = "No"
                End IF
                If CInt(allowInvoiceServiceAccountBalance) = 1 Then 
                    allowInvoiceServiceAccountBalance = "Yes"
                Else
                    allowInvoiceServiceAccountBalance = "No"
                End IF
                If CInt(allowInvoiceAdminFee) = 1 Then 
                    allowInvoiceAdminFee = "Yes"
                Else
                    allowInvoiceAdminFee = "No"
                End IF
                If rowNumber = 0 Then 
                    strGrey = " bgcolor='#eeeeee'"
                    rowNumber = 1
                Else
                    strGrey = ""
                    rowNumber = 0
                End IF
                Response.Write("<tr" & strGrey & ">")
            Response.Write("<td><a href='settingsProduct.aspx?productID=" & productID & "'>" & product & "</a></td>")
            Response.Write("<td>" & partnerSubvention & "</td>")
                Response.Write("<td>" & country & "</td>")
                Response.Write("<td>" & riskCarrier & "</td>")
                Response.Write("<td>" & allowInvoiceDistancePenalty & "</td>")
                Response.Write("<td>" & allowInvoiceDurationPenalty & "</td>")
                Response.Write("<td>" & allowInvoiceServiceAccountBalance & "</td>")
                Response.Write("<td>" & allowInvoiceAdminFee & "</td>")
                Response.Write("<td>" & vehicleCount & "</td>")
                Response.Write("<td align='right'><a href='settingsPriceList.aspx?productID=" & productID & "'>Edit price list</a></td>")
                Response.Write("<td align='right'><a href='settingsAdditionalProduct.aspx?productID=" & productID & "'>Edit additional products</a></td>")
                Response.Write("<td align='right'><a href='copyProduct.aspx?productID=" & productID & "'>Copy product</a></td>")
                Response.Write("</tr>")
                RecSet.MoveNext()
            Loop
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        Response.Write("<tr><td colspan='11'><hr class='hrHeadline'></td></tr>")
        Response.Write("</table>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Response.Write("</table>")
        Under()
    ElseIf Request.QueryString("do") = "save" Then 
        countryID = Request.Form("countryID")
        product = Request.Form("product")
        riskCarrier = Request.Form("riskCarrier")
        allowInvoiceDistancePenalty = Request.Form("allowInvoiceDistancePenalty")
        allowInvoiceDurationPenalty = Request.Form("allowInvoiceDurationPenalty")
        allowInvoiceServiceAccountBalance = Request.Form("allowInvoiceServiceAccountBalance")
        allowInvoiceAdminFee = Request.Form("allowInvoiceAdminFee")
    productID = Request.QueryString("productID")
    partnerSubvention = Request.Form("partnerSubvention")
        countryID = CLng(countryID)
        If Len(product) > 0 Then 
            product = Trim(product)
            product = Replace(CStr(product), "'", "")
            product = Replace(CStr(product), """", "")
            product = Replace(CStr(product), "<", "")
            product = Replace(CStr(product), ">", "")
            product = Replace(CStr(product), "=", "")
        End IF
        riskCarrier = CLng(riskCarrier)
        If CInt(riskCarrier) = 2 Then 
            riskCarrier = 0
        End IF
        allowInvoiceDistancePenalty = CLng(allowInvoiceDistancePenalty)
        If CInt(allowInvoiceDistancePenalty) = 2 Then 
            allowInvoiceDistancePenalty = 0
        End IF
        allowInvoiceDurationPenalty = CLng(allowInvoiceDurationPenalty)
        If CInt(allowInvoiceDurationPenalty) = 2 Then 
            allowInvoiceDurationPenalty = 0
        End IF
        allowInvoiceServiceAccountBalance = CLng(allowInvoiceServiceAccountBalance)
        If CInt(allowInvoiceServiceAccountBalance) = 2 Then 
            allowInvoiceServiceAccountBalance = 0
        End IF
        allowInvoiceAdminFee = CLng(allowInvoiceAdminFee)
        If CInt(allowInvoiceAdminFee) = 2 Then 
            allowInvoiceAdminFee = 0
    
        If (partnerSubvention = "") Then
            partnerSubvention = CLng(0)
        End If
        
        partnerSubvention = CLng(partnerSubvention)
       
    
    End If
    
    
    If Len(productID) > 0 Then
        productID = CLng(productID)
        dbOpen()
        Conn.Execute("Update tblProduct Set countryID = " & countryID & ", product = '" & product & "', riskCarrier = " & riskCarrier & ", allowInvoiceDistancePenalty = " & allowInvoiceDistancePenalty & ", allowInvoiceDurationPenalty = " & allowInvoiceDurationPenalty & ", partnerSubvention = " & partnerSubvention & ", allowInvoiceServiceAccountBalance = " & allowInvoiceServiceAccountBalance & ", allowInvoiceAdminFee = " & allowInvoiceAdminFee & " Where productID = " & productID & "")
        dbClose()
    Else
        dbOpen()
        str = "Insert Into tblProduct (countryID, product, riskCarrier, allowInvoiceDistancePenalty, allowInvoiceDurationPenalty, allowInvoiceServiceAccountBalance, allowInvoiceAdminFee,partnerSubvention) Values(" & countryID & ",'" & product & "'," & riskCarrier & "," & allowInvoiceDistancePenalty & "," & allowInvoiceDurationPenalty & "," & allowInvoiceServiceAccountBalance & "," & allowInvoiceAdminFee & "," & partnerSubvention & ")"
        Conn.Execute(str)
        dbClose()
    End If
    Response.Redirect("settingsProduct.aspx?subDo=%20-%20SAVED")
    ElseIf Request.QueryString("do") = "delete" Then
        productID = Request.QueryString("productID")
        productID = CLng(productID)
        dbVehicleCount = 1
        dbOpen()
        RecSet = Conn.Execute("Select Count(productID) As vehicleCount From tblVehicle Where productID = " & productID & "")
        dbVehicleCount = RecSet.Fields("vehicleCount").Value
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        If CInt(dbVehicleCount) = 0 And CDbl(fragusLeasingUserTypeID) = 1 Then
            dbOpen()
            Conn.Execute("Delete From tblPriceList Where productID = " & productID & "")
            Conn.Execute("Delete From tblAdditionalProduct Where productID = " & productID & "")
            Conn.Execute("Delete From tblProduct Where productID = " & productID & "")
            dbClose()
        End If
        Response.Redirect("settingsProduct.aspx?subDo=%20-%20DELETED")
    End If
%>
